﻿using PeiYue.IRepository.Base;
using PeiYue.IServices.Store;
using PeiYue.Model.Models.Store;
using PeiYue.Services.BASE;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace PeiYue.Services.Store
{
    public class PickProgressServices : BaseServices<PickProgressDao>, IPickProgressServices
    {
        IBaseRepository<PickProgressDao> _dal;
        public PickProgressServices(IBaseRepository<PickProgressDao> dal)
        {
            this._dal = dal;
            base.BaseDal = dal;
        }
        /// <summary>
        /// 获取分拣进度
        /// </summary>
        /// <param name="isNow">true查预计发货>=当天<明天，false查预计发货>=明天</param>
        /// <returns></returns>
        public async Task<List<PickProgressDao>> GetPickPickProgresList(bool isNow)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("Select h.预计发货,(isnull(b.拣选数,0) + ISNULL(b.扣库数,0)) AS 拣选数, c.分拣方式,l.制单日期 AS 分拣日期   ");
            strSql.Append("from v销售订单表头 (Nolock)  h   ");
            strSql.Append("Left Join v销售订单表体 (Nolock)  b  ON h.单号 = b.单号   ");
            strSql.Append("Left Join v存货档案 (Nolock)  c ON b.存货ID = c.fID ");
            strSql.Append("Left Join v仓库拣选日志 (Nolock)  l ON b.fID = l.销售ID  ");
            strSql.Append("Where h.状态 = 1  And h.确认日期 IS NOT NULL  ");
            strSql.Append("And h.类型 In('正常订单','调料订单') 	and c.是否附随品!='是'   ");

            List<SugarParameter> sugarParameters = new List<SugarParameter>();
            var today = DateTime.Now.Date;
            var tomorrowDate = today.AddDays(1).Date;
            var afterTomorrowDate = tomorrowDate.AddDays(1).Date;
            if (isNow)
            {
                strSql.Append("and h.预计发货>=@tomorrowDate and h.预计发货 <@afterTomorrowDate  ");
            }
            else
            {
                strSql.Append("and h.预计发货>=@today and h.预计发货<@tomorrowDate ");
            }
            sugarParameters.Add(new SugarParameter("@today", today));
            sugarParameters.Add(new SugarParameter("@tomorrowDate", tomorrowDate));
            sugarParameters.Add(new SugarParameter("@afterTomorrowDate", afterTomorrowDate));
            return await _dal.SqlQueryList<PickProgressDao>(strSql.ToString(), sugarParameters);
        }
        /// <summary>
        /// 获取销售金额
        /// </summary>
        /// <returns></returns>
        public async Task<int> GetSalesAmount(bool isNow)
        {
            StringBuilder strSql =new StringBuilder();
            strSql.Append("Select isnull(值,0) as Amount  from [系统].[dbo].[仪表盘数据] With (Nolock) Where 类型 = '销售额' ");
            //strSql.Append("and 制单日期>=@createDate ");
            //var createDate = new DateTime();
            //if (isNow)
            //{
            //    createDate= DateTime.Now.AddDays(1).Date;
            //}
            //else
            //{
            //    createDate = DateTime.Now.Date;
            //}
            var dt = await _dal.GetDataTableAsync(strSql.ToString(), null);
            if (dt.Rows.Count > 0)
            {
                return Convert.ToInt32(Convert.ToDecimal(dt.Rows[0]["Amount"]));
            }
            return 0;
        }
        /// <summary>
        /// 获取供拣总包裹数
        /// </summary>
        /// <param name="isNow">true查预计发货>=当天<明天，false查预计发货>=明天</param>
        /// <returns></returns>
        public async Task<int> GetSupplierSortTotal(bool isNow)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("  Select count(1) as Total ");
            strSql.Append(" from v销售订单表头(Nolock)  h ");
            strSql.Append(" Left Join v销售订单表体(Nolock)  b ON h.单号 = b.单号 ");
            strSql.Append(" Left Join 采购订单表体(Nolock)  p ON b.fID = p.销售ID ");
            strSql.Append(" Left Join v存货档案(Nolock)  c ON b.存货ID = c.fID ");
            strSql.Append(" Left Join v仓库拣选日志(Nolock)  l ON b.fID = l.销售ID ");
            strSql.Append(" Where h.状态 = 1  And h.确认日期 IS NOT NULL ");
            strSql.Append(" And h.类型 In('正常订单','调料订单') 	and c.是否附随品 != '是' ");
            strSql.Append(" and c.分拣方式 = '供应商分拣' ");
            strSql.Append(" and isnull(l.包裹号,1)> 0 ");
            strSql.Append(" and p.数量 > 0 and p.状态 = 1 ");
            strSql.Append(" and isnull(p.扣库数,0)= 0  ");
            List<SugarParameter> sugarParameters = new List<SugarParameter>();
            var today = DateTime.Now.Date;
            var tomorrowDate = today.AddDays(1).Date;
            var afterTomorrowDate = tomorrowDate.AddDays(1).Date;
            if (isNow)
            {
                strSql.Append("and h.预计发货>=@tomorrowDate and h.预计发货 <@afterTomorrowDate  ");
            }
            else
            {
                strSql.Append("and h.预计发货>=@today and h.预计发货<@tomorrowDate ");
            }
            sugarParameters.Add(new SugarParameter("@today", today));
            sugarParameters.Add(new SugarParameter("@tomorrowDate", tomorrowDate));
            sugarParameters.Add(new SugarParameter("@afterTomorrowDate", afterTomorrowDate));
            var dt = await _dal.GetDataTableAsync(strSql.ToString(), sugarParameters);
            if (dt.Rows.Count > 0)
            {
                return Convert.ToInt32(Convert.ToDecimal(dt.Rows[0]["Total"]));
            }
            return 0;
        }
        /// <summary>
        /// 获取订单金额
        /// </summary>
        /// <returns></returns>
        public async Task<decimal>GetOrderAmount(bool isNow)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("  Select isnull(sum(b.订单金额),0) as Total ");
            strSql.Append(" from v销售订单表头(Nolock)  h ");
            strSql.Append(" Left Join v销售订单表体(Nolock)  b ON h.单号 = b.单号 ");
            List<SugarParameter> sugarParameters = new List<SugarParameter>();
            var today = DateTime.Now.Date;
            var tomorrowDate = today.AddDays(1).Date;
            var afterTomorrowDate = tomorrowDate.AddDays(1).Date;
            if (isNow)
            {
                strSql.Append("and h.预计发货>=@tomorrowDate and h.预计发货 <@afterTomorrowDate  ");
            }
            else
            {
                strSql.Append("and h.预计发货>=@today and h.预计发货<@tomorrowDate ");
            }
            sugarParameters.Add(new SugarParameter("@today", today));
            sugarParameters.Add(new SugarParameter("@tomorrowDate", tomorrowDate));
            sugarParameters.Add(new SugarParameter("@afterTomorrowDate", afterTomorrowDate));
            var dt = await _dal.GetDataTableAsync(strSql.ToString(), sugarParameters);
            if (dt.Rows.Count > 0)
            {
                return Convert.ToDecimal(dt.Rows[0]["Total"].ToString());
            }
            return 0;
        }
    }
}
